package servlet;

import java.io.IOException;import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biz.CustomerBiz;
import biz.DriverStateBiz;
import biz.OrderBiz;
import biz.OrderDetailBiz;
import biz.impl.CustomerBizImpl;
import biz.impl.DriverStateBizImpl;
import biz.impl.OrderBizImpl;
import biz.impl.OrderDetailBizImpl;
import entity.Address;
import entity.CartItem;
import entity.Customer;
import entity.Driver;
import entity.DriverState;
import entity.Order;
import entity.OrderDetail;
import entity.OrderState;

/**
 * Servlet implementation class ShoppingCartSubmitOrderServlet
 */
@WebServlet("/ShoppingCartSubmitOrder.html")
public class ShoppingCartSubmitOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String addrIdStr = request.getParameter("addrId");
		int addrId=0;
		if(addrIdStr!=null&&!addrIdStr.isEmpty()){
			addrId=Integer.valueOf(addrIdStr);
		}
		
		List<CartItem> list = (List<CartItem>)request.getSession().getAttribute("selectedOrderList");
		if(list!=null&&!list.isEmpty()){
			OrderDetailBiz orderDetailBiz = new OrderDetailBizImpl();
			OrderDetail detail=null;
			String orderParentId = "oz"+System.currentTimeMillis();
			double totalPrice=0;
			for (CartItem cartItem : list) {
				detail=new OrderDetail(0, cartItem.getRate(), cartItem.getCount(), orderParentId, cartItem.getUnitPrice());
				orderDetailBiz.addOrderDetail(detail);
				totalPrice+=cartItem.getUnitPrice();
			}
			
			//订单增加
			OrderBiz orderBiz= new OrderBizImpl();
			Order order = new Order();
			
			Address address=new Address();
			address.setAddrid(addrId);
			order.setAddress(address);
			
			DriverStateBiz stateBiz =  new DriverStateBizImpl();
			DriverState driverState = stateBiz.queryDriverOfDriverStateByDriStateId(3);
			Driver driver = driverState.getDriverList().get(0);
			order.setDriver(driver);
			
			order.setOrderDate(new Date(new java.util.Date().getTime()));
			order.setOrderParentId(orderParentId);
			
			OrderState orderState=new OrderState();
			orderState.setOrderStateId(1);
			order.setOrderState(orderState);
			
			order.setTotalPrice(totalPrice);
			
			orderBiz.addOrder(order);
			
			Customer cus = (Customer)request.getSession().getAttribute("user");
			CustomerBiz cusBiz = new CustomerBizImpl();
			Customer newCus = cusBiz.queryCustomerInfo(cus.getCusId());
			request.getSession().setAttribute("user", newCus);
			
			response.sendRedirect(request.getContextPath()+"/MyCenter.html");
		}
		response.getWriter().println("<h1 style='color:white;'>提交订单失败</h1>");
		response.getWriter().close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
